//
//  ADRetweetView.swift
//  myBlog
//
//  Created by mac2014 on 16/3/30.
//  Copyright © 2016年 AD. All rights reserved.
//

import UIKit
import SnapKit

/// 被转发的微博view
class ADRetweetView: UIView {
    //MARK: - 属性
    var retweetViewBottomCons: Constraint?
    
    var retweetStatus: ADStatus? {
        //当给retweetStatus赋值时，执行下面{}
        didSet {
            //contentLabel
            guard let userName = retweetStatus?.user?.screen_name else {
                return
            }
            guard let text = retweetStatus?.text else {
                return
            }
            //转发微博的内容格式：用户名+微博正文内容
            contentLabel.text = "@\(userName):\(text)"
            //print("转发内容：\(contentLabel.text)")
            //picView
            retweetViewBottomCons?.uninstall()
            if let picURLs = retweetStatus?.storePictureURLs {
                //有图
                picView.picURLs = picURLs
                
                picView.hidden = false
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomCons = make.bottom.equalTo(picView).offset(8).constraint
                })
            } else {
                //无图
                picView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomCons = make.bottom.equalTo(contentLabel).offset(8).constraint
                })
            }
        }
    }
    //MARK: - 系统方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 自定义方法
    private func prepareUI() {
        addSubview(bgBtn)
        addSubview(contentLabel)
        addSubview(picView)
        
        // 添加约束
        /// 背景按钮
        bgBtn.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        /// 被转发文本
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(8)
            make.trailing.equalTo(self).offset(-8)
        }
        
        /// 被转发的微博配图
        picView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottomCons = make.bottom.equalTo(picView).offset(8).constraint
        }
    }
    
    //MARK: - 懒加载
    //背景按钮
    private lazy var bgBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "retweet_background_hightlighted"), forState: UIControlState.Highlighted)
        return btn
    }()
    //文本内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(16)
        label.numberOfLines = 0
        return label
    }()
    //微博配图
    private lazy var picView: ADPictureView = ADPictureView()
}
